<template>
	<view class="address">
		<view class="address_list">
			<u-swipe-action v-if="!selectAddress">
				<view class="address_item" v-for="(item,i) in addressList" :key="i">
					<u-swipe-action-item :options="options" @click="handleDel(item.id)" :name="i">
						<view class="address_slot">
							<view class="address_left">
								<view class="user_info">
									<text class="name">{{item.name}}</text><text>{{item.phone}}</text>
									<text class="default" v-if="item.default_set === '1'">默认</text>
								</view>
								<view class="address_info">
									{{`${item.area_name} ${item.desc}`}}
								</view>
							</view>
							<view class="address_right iconfont" @click="goAddAddress(item)">&#xe600;</view>
						</view>
					</u-swipe-action-item>
				</view>
			</u-swipe-action>
			<view class="address_item" v-else v-for="(item,i) in addressList" :key="i" @click="goSettle(item)">
				<view class="address_slot">
					<view class="address_left">
						<view class="user_info">
							<text class="name">{{item.name}}</text><text>{{item.phone}}</text>
							<text class="default" v-if="item.default_set === '1'">默认</text>
						</view>
						<view class="address_info">
							{{`${item.area_name} ${item.desc}`}}
						</view>
					</view>
					<view class="address_right iconfont" @click.stop="goAddAddress(item)">&#xe600;</view>
				</view>
			</view>
		</view>
		<view class="address_bottom">
			<view class="address_add" @click="goAddAddress">+新增收货地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectAddress: false,
				addressList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: "#f56c6c",
					}
				}]
			}
		},
		onLoad(options) {
			if (options.selectAddress) {
				this.selectAddress = options.selectAddress
			}
		},
		onShow() {
			this.getAddress()
		},
		methods: {
			getAddress() {
				uni.$http.get('/api/address').then(res => {
					this.addressList = res.data.result
				})
			},
			goAddAddress(item) {
				uni.navigateTo({
					url: `/address/add_address?id=${item.id}&name=${item.name}&phone=${item.phone}&area_name=${item.area_name}&area=${item.area}&desc=${item.desc}&default_set=${item.default_set}&`
				})
			},
			handleDel(id) {
				uni.$http.delete(`/api/address/${id}`).then(res => {
					this.getAddress()
				})
			},
			goSettle(data) {
				uni.navigateBack({
					success: () => {
						uni.$emit('selected', data)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.address {
		background-color: #f1f1f1;
		// padding-bottom: 50px;
		height: 100%;

		.address_list {
			display: flex;
			flex-direction: column;
			padding: 7px;

			.address_item {
				border-radius: 7px;
				margin-bottom: 7px;
				overflow: hidden;

				.address_slot {
					background-color: #fff;
					padding: 15px;
					font-size: 14px;
					display: flex;
					align-items: center;
					justify-content: space-between;
					z-index: 1;

					.address_left {
						padding-right: 40px;
						flex-grow: 1;

						.user_info {
							font-weight: 600;
							padding-bottom: 7px;
							display: flex;
							align-items: center;

							.name {
								margin-right: 7px;
							}

							.default {
								color: #884e22;
								font-weight: 400;
								font-size: 12px;
								padding: 2px;
								border: 1px solid #884e22;
								border-radius: 5px;
								margin-left: 10px;
							}
						}
					}

					.address_right {
						font-size: 20px;
						padding-right: 7px;
					}
				}
			}

		}

		.address_bottom {
			width: 100%;
			text-align: center;
			line-height: 40px;
			background-color: #fff;
			position: fixed;
			bottom: -1px;
			padding: 7px 10px;
			box-sizing: border-box;
			z-index: 999;

			.address_add {
				background-color: #884e22;
				color: #fff;
				border-radius: 20px;
			}
		}
	}
</style>